package webSocket.channel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.stereotype.Component;

import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

@Component
public class QQ_ChannelGroup {

	private volatile ChannelGroup channelGroup = new DefaultChannelGroup("qq_group", GlobalEventExecutor.INSTANCE);
	private volatile List<ChannelId> channelIds = new ArrayList<ChannelId>();
	private volatile AtomicInteger groupCount = new AtomicInteger(0);
	
	/**
	 * 添加channel
	 * @param channel
	 * @return 返回当前channel个数
	 */
	public Integer addChannel(Channel channel) {
		channelIds.add(channel.id());
		channelGroup.add(channel);
		return groupCount.incrementAndGet();
	}
	
	/**
	 * 删除channel
	 * @param channel
	 * @return 返回当前channel个数
	 */
	public Integer removeChannel(Channel channel) {
		channelIds.remove(channel.id());
		channelGroup.remove(channel);
		return groupCount.decrementAndGet();
	}
	
	
	
	
	public ChannelGroup getChannelGroup() {
		return channelGroup;
	}
	public void setChannelGroup(ChannelGroup channelGroup) {
		this.channelGroup = channelGroup;
	}

	public AtomicInteger getGroupCount() {
		return groupCount;
	}

	public List<ChannelId> getChannelIds() {
		return channelIds;
	}
	
	
}
